package co.yixiang.business.comment

import co.yixiang.annotation.AnonymousAccess
import co.yixiang.api.ApiResult
import co.yixiang.entity.Comment
import co.yixiang.entity.Comments
import co.yixiang.modules.user.service.YxUserService
import co.yixiang.util.getApiResult
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import me.liuwj.ktorm.database.Database
import me.liuwj.ktorm.dsl.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.lang.Exception

interface CommentService {

    /**
     * 添加评论
     */
    fun insertComment(comments: Comments): ApiResult<*>


    /**
     * 移除评论内容
     */
    fun removeComment(ids: String): ApiResult<*>


    /**
     * 获取某个帖子的全部评论信息
     */
    fun getCommentVoListByPostId(postId: Long): List<CommentVo>?


    /**
     * 获取评论信息
     */
    fun getCommentVo(commentId: Long): CommentVo
}

@Service
@Transactional(rollbackFor = [Exception::class])
open class CommentServiceImp(@Autowired val database: Database, @Autowired val userService: YxUserService) : CommentService {

    override fun insertComment(comments: Comments): ApiResult<*> {

        return database.insert(Comment) {

            set(it.content, comments.content)
            set(it.pid, comments.pid ?: 0)
            set(it.postId, comments.postId)
            set(it.type, comments.type ?: 0)
            set(it.createBy, comments.createBy)
            set(it.createTime, comments.createTime)
            set(it.deleted, comments.deleted)
        }.run { getApiResult(200, "添加评论成功") }
    }

    override fun removeComment(ids: String): ApiResult<*> {

       return ids.split(",").forEach { i -> database.delete(Comment) {it.id eq i.toLong()} }
           .run { getApiResult(200, "移除成功") }
    }

    override fun getCommentVoListByPostId(postId: Long): List<CommentVo> {

       return database.from(Comment).select(Comment.id).where { Comment.postId.eq(postId) }
            .orderBy(Comment.createTime.desc()).map { row -> getCommentVo(row[Comment.id]!!) }.toList()
    }

    override fun getCommentVo(commentId: Long): CommentVo {

        val commentCreate = Comment.aliased("commentCreate")
        val commentCreateCreateBy = commentCreate.createBy.aliased("commentCreateCreateBy")
        val commentReply = Comment.aliased("commentReply")
        val commentReplyCreateBy = commentReply.createBy.aliased("commentReplyCreateBy")

        return database.from(commentCreate).leftJoin(commentReply, on = commentCreate.pid.eq(commentReply.id))
            .select(commentCreate.content, commentCreateCreateBy, commentCreate.createTime, commentReplyCreateBy, commentCreate.id, commentCreate.createTime)
            .where { commentCreate.id.eq(commentId) }
            .map { row -> CommentVo(

                id = row[commentCreate.id],
                content = row[commentCreate.content],
                userId = row[commentCreateCreateBy],
                replyUserId = row[commentReplyCreateBy],
                userName = userService.getById(row[commentCreateCreateBy])?.realName,
                replyUserName = userService.getById(row[commentReplyCreateBy])?.realName,
                createTime = row[commentCreate.createTime]
            ) }[0]
    }
}

@Api(tags = ["评论"])
@RestController
@RequestMapping("/comment")
class CommentController(@Autowired val commentService: CommentService) {

    @ApiOperation("添加评论")
    @PostMapping("/insertComment")
    @AnonymousAccess
    fun insertComment(comments: Comments): ApiResult<*> = commentService.insertComment(comments)

    @ApiOperation("移除评论")
    @PostMapping("/removeComment")
    @AnonymousAccess
    fun removeComment(ids: String): ApiResult<*> = commentService.removeComment(ids)
}
